package com.haotunet.app.goldtreasure.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;

public class BuyRecordFragment extends BaseFragment implements OnClickListener {
    private View view;
    private TextView tvCurrent, tvRegular, tvG20;
    private String mType;
    private boolean isPrepared;
    private boolean mHasLoadedOnce;
    private ShareStore shareStore;

    public static BuyRecordFragment newInstance(String type) {
        Bundle args = new Bundle();
        args.putString("type", type);
        BuyRecordFragment fragment = new BuyRecordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_buy_record, null);
        shareStore = new ShareStore(getActivity());
        initView();
        isPrepared = true;
        lazyLoad();
        return view;
    }

    private void initView() {
        mType = getArguments().getString("type");
        tvCurrent = (TextView) view.findViewById(R.id.tv_current);
        tvRegular = (TextView) view.findViewById(R.id.tv_regular);
        tvG20 = (TextView) view.findViewById(R.id.tv_g20);
        if (!shareStore.getBoolean(Strs.IS_SHOW_G20, false)) {
            tvG20.setVisibility(View.GONE);
        }
        if (mType.equals("transSuccess") || mType.equals("buyRegularSuccess")) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.buy_record_layout, CurrentRecordFragment.newInstance(2)).commitAllowingStateLoss();
            tvRegular.setSelected(true);
        } else {
            getFragmentManager().beginTransaction()
                    .replace(R.id.buy_record_layout, CurrentRecordFragment.newInstance(1)).commitAllowingStateLoss();
            tvCurrent.setSelected(true);
        }
        tvCurrent.setOnClickListener(this);
        tvRegular.setOnClickListener(this);
        tvG20.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_current:
                tvCurrent.setSelected(true);
                tvRegular.setSelected(false);
                tvG20.setSelected(false);
                getFragmentManager().beginTransaction()
                        .replace(R.id.buy_record_layout, CurrentRecordFragment.newInstance(1)).commitAllowingStateLoss();
                break;
            case R.id.tv_regular:
                tvCurrent.setSelected(false);
                tvRegular.setSelected(true);
                tvG20.setSelected(false);
                getFragmentManager().beginTransaction()
                        .replace(R.id.buy_record_layout, CurrentRecordFragment.newInstance(2)).commitAllowingStateLoss();
                break;
            case R.id.tv_g20:
                tvCurrent.setSelected(false);
                tvRegular.setSelected(false);
                tvG20.setSelected(true);
                getFragmentManager().beginTransaction()
                        .replace(R.id.buy_record_layout, CurrentRecordFragment.newInstance(3)).commitAllowingStateLoss();
                break;
            default:
                break;
        }
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
    }
}
